// import babel from "@rollup/plugin-babel"
// import { DEFAULT_EXTENSIONS } from "@babel/core"

import copy from 'rollup-plugin-copy'
import { terser } from "rollup-plugin-terser"
import commonjs from "@rollup/plugin-commonjs"
import resolve from "@rollup/plugin-node-resolve"
import rollupTypescript from "rollup-plugin-typescript2"

const plugins = [
  // 解析第三方依赖
  resolve(),

  // 识别 commonjs 模式第三方依赖
  commonjs(),

  rollupTypescript(),

  // babel({
  //   // 编译库使用 runtime
  //   babelHelpers: "runtime",
  //   // 只转换源代码，不转换外部依赖
  //   exclude: "node_modules/**",
  //   // babel 默认不支持 ts 需要手动添加
  //   extensions: [...DEFAULT_EXTENSIONS, ".ts"],
  // }),

  copy({
    targets: [
      { src: 'src/package.json', dest: 'dist' },
      { src: 'src/@types/*.d.ts', dest: 'dist/types' },
    ],
  }),
]

const env = process.env.NODE_ENV

// 若打包正式环境，压缩代码
if (env === "production") {
  plugins.push(
    terser({
      compress: {
        pure_getters: true,
        unsafe: true,
        unsafe_comps: true,
        warnings: false,
      },
    })
  );
}

export default {
  input: 'src/index.ts',

  output: {
    preserveModules: true,
    dir: 'dist'
  },

  plugins
};
